// pages/activities/activities.js
const app = getApp()
var wh = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: ['全部', '美食', '酒店', "美妆", '家政', '汽车', "园艺", '文体', '饮品'],
    currentTab: 0,
    activities_list:[],
    onearth: false, // 是否已经到达底部
    page: 1,
    page_size: 5,
    no_data: 0,
    catId:'',
    have: true,
  },
  //响应点击导航栏
  navbarTap: function (e) {
    console.log(e.currentTarget.dataset)
    var that = this;
    this.setData({
      activities_list: [],
      page: 1,
      page_size: 5,
      no_data: 0,
      onearth: false,
      have: true,
      currentTab: e.currentTarget.dataset.idx,
      catId: e.currentTarget.dataset.cat_id,
    });
    that.special(e.currentTarget.dataset.cat_id)
  }, 
  // 分类
  getShopCategory: function (catId) {
    let that = this;
    wh.Post('/api/mall.shop/category', {
      par_id: catId
    }, function (res) {
      console.log('分类', res)
      that.setData({
        navbar: res
      })
    });
  },
  // 获取专题活动列表
  special(catId) {
    let that = this;
    let page_size = this.data.page_size,
      no_data = this.data.no_data,
      page = this.data.page;
    if (no_data == 0) {
      wh.Post('/api/mall.activity/lists', {
        page: page,
        page_num: page_size,
        cat_id: catId
      }, function (res) {
        console.log(res)
        let activities_list = that.data.activities_list.concat(res),
          count = res.length;
        console.log(res)
        if (page_size > count) {
          that.data.no_data = 1;
          if (activities_list.length > 3) {
            that.setData({
              onearth: true
            })
          }
        }
        that.setData({
          activities_list: activities_list
        })
        if (that.data.activities_list.length == 0) {
          that.setData({
            have: false,
          });
        } else {
          that.setData({
            have: true,
          });
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.special(1)
    this.getShopCategory(0)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let no_data = this.data.no_data,
      page = this.data.page,
      page_size = this.data.page_size;
    if (no_data == 0) {
      this.data.page = page + 1;
      this.special(this.data.catId)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})